<?php

App::uses('AppController', 'Controller');

/**
 * Banners Controller
 *
 * @property Banner $Banner
 * @property PaginatorComponent $Paginator
 */
class BannersController extends AppController {

    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator');

    /**
     * index method
     *
     * @return void
     */
    public function admin_index() {
        $this->Banner->recursive = 0;
        $this->Paginator->settings = array(
            'limit' => 5
        );
        $this->set('banners', $this->Paginator->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Banner->exists($id)) {
            throw new NotFoundException(__('Invalid banner'));
        }
        $options = array('conditions' => array('Banner.' . $this->Banner->primaryKey => $id));
        $this->set('banner', $this->Banner->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->Banner->create();
            $files = $this->request->data['Banner']['file_name'];
            move_uploaded_file($files['tmp_name'], WWW_ROOT . '/img/banner/' . $files['name']);
            $this->request->data['Banner']['file_name'] = $files['name'];
            $this->request->data['Banner']['create_date'] = date("Y-m-d");
            $this->request->data['Banner']['full_path'] = $this->webroot . "img/banner/" . $files['name'];
            $this->request->data['Banner']['file_name'] = $files['name'];
            if ($this->Banner->save($this->request->data)) {
                $this->Session->setFlash(__('Thêm thành công '), 'backend/green', array('redirect' => ''));
                return $this->redirect(array('action' => 'admin_index'));
            } else {
                $this->Session->setFlash(__('Could not be saved. Please, try again.'));
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        if (!$this->Banner->exists($id)) {
            throw new NotFoundException(__('Invalid banner'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            $files = $this->request->data['Banner']['file_name'];
            move_uploaded_file($files['tmp_name'], WWW_ROOT . '/img/banner/' . $files['name']);
            $this->request->data['Banner']['file_name'] = $files['name'];
            $this->request->data['Banner']['update_date'] = date("Y-m-d");
            $this->request->data['Banner']['full_path'] = $this->webroot . "img/banner/" . $files['name'];
            $this->request->data['Banner']['file_name'] = $files['name'];
            if ($this->Banner->save($this->request->data)) {
                $this->Session->setFlash(__('Cập nhật thành công '), 'backend/green', array('redirect' => ''));
                return $this->redirect(array('action' => 'admin_index'));
            } else {
                $this->Session->setFlash(__('Could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Banner.' . $this->Banner->primaryKey => $id));
            $this->request->data = $this->Banner->find('first', $options);
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        $this->Banner->id = $id;
        if (!$this->Banner->exists()) {
            throw new NotFoundException(__('Invalid banner'));
        }
        $this->request->onlyAllow('get', 'admin_delete');

        if ($this->Banner->delete()) {
            $this->Session->setFlash(__('Xóa thành công'), 'backend/green', array('redirect' => ''));
        } else {
            $this->Session->setFlash(__('Lỗi xóa dữ liệu'), 'backend/red', array('redirect' => ''));
        }
        return $this->redirect(array('action' => 'admin_index'));
    }

    public function getAllBanner() {
        $banner = $this->Banner->find('all');
        return $banner;
    }

}
